package com.ciwong.epaper.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.ciwong.epaper.R;
import com.ciwong.epaper.util.ViewUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 倒计时
 * Created by lzr on 2017/4/18.
 */
public class CountDownView extends View {

    private Context context;
    private Paint paint;
    private List<Bitmap> listBit = new ArrayList<>();
    private int index;

    //动画的时间长度
    private int timeCount = 3000;
    //旋转幅度
    private int countX = -1;
    private int countSec = 0;

    private boolean isPlay = true;

    Handler handler = new Handler();


    public CountDownView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        paint = new Paint();
        listBit.add(BitmapFactory.decodeResource(context.getResources(), R.mipmap.count_3));
        listBit.add(BitmapFactory.decodeResource(context.getResources(), R.mipmap.count_2));
        listBit.add(BitmapFactory.decodeResource(context.getResources(), R.mipmap.count_1));
        timeOut();
    }

    public void setTimeCount(int timeCount) {
        this.timeCount = timeCount;
    }

    private void timeOut() {
        if (timeCount <= 0) {
//                closeView();
        } else {
            if (timeCount != 3000) {
                index++;
                if (timeCount != 1000) {
                    countX = 5;
                }
                invalidate();
            }
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    timeCount = timeCount - 1000;
                    timeOut();
                }
            }, 1000);
        }
    }

    ;


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isPlay) {
            float xPoint = getWidth() / 2;//圆的中心坐标x
            float yPoint = getHeight() / 2;//圆的中心坐标y
            float radius = getWidth() / 2;//圆的半径
            paint.setAntiAlias(true);
            paint.setColor(context.getResources().getColor(R.color.white));
            //画外面的圆
            canvas.drawCircle(xPoint, yPoint, radius, paint);
            //画第二层圆
            paint.setColor(Color.parseColor("#ffff9d"));
            float seRadius = radius - ViewUtil.dip2px(context, 6);
            canvas.drawCircle(xPoint, yPoint, seRadius, paint);
            //画第三层圆
            paint.setColor(Color.parseColor("#beeb9f"));
            float thRadius = seRadius - ViewUtil.dip2px(context, 10);
            canvas.drawCircle(xPoint, yPoint, thRadius, paint);
            //画弧

            float left = ViewUtil.dip2px(context, 6) + ViewUtil.dip2px(context, 10);
            RectF rectF = new RectF(left, left, getWidth() - left, getHeight() - left);
            paint.setColor(Color.parseColor("#00a388"));
            if (countX > 0 && countX < 90) {
                canvas.drawArc(rectF, 0, 90, true, paint);
                paint.setColor(Color.parseColor("#beeb9f"));
                canvas.drawArc(rectF, 0, 0 + countX, true, paint);
                paint.setColor(Color.parseColor("#79bd8f"));
                canvas.drawArc(rectF, 90, 90 + countX, true, paint);
                countX = countX + 5;
                invalidate();
            } else {
                canvas.drawArc(rectF, 0, 90 - countX, true, paint);
                paint.setColor(Color.parseColor("#79bd8f"));
                canvas.drawArc(rectF, 90, 90 + countX, true, paint);
            }
            if (index == (listBit.size() - 1)) {
                if (countSec < 180) {
                    paint.setColor(Color.parseColor("#beeb9f"));
                    canvas.drawArc(rectF, 90, 0 + countSec, true, paint);
                    countSec = countSec + 5;
                    invalidate();
                } else {
                    paint.setColor(Color.parseColor("#beeb9f"));
                    canvas.drawArc(rectF, 90, 0 + countSec, true, paint);
                }
            }
            if (index < listBit.size()) {
                canvas.drawBitmap(listBit.get(index), (xPoint - listBit.get(index).getWidth() / 2), (yPoint - listBit.get(index).getHeight() / 2), paint);
            }
        }
    }

    public void destroy() {
        isPlay=false;
        for (int i = 0; i < listBit.size(); i++) {
            listBit.get(i).recycle();
        }
    }

}
